/*
 *  POJ3009: Curling 2.0
 */

/*-------- Includes --------*/
#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

/*-------- Types --------*/

/*-------- Global Variables --------*/
bool board[20][20];
int  w,  h;
int  sx, sy;
int  ex, ey;

vector<int> rslt;

/*-------- Function Prototypes --------*/
void dfs(int x, int y, int depth);

/*-------- Main Function --------*/
int main(int argc, char const *argv[])
{
    // get board
    while (cin >> w >> h && w) {
        int t;

        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                cin >> t;

                switch (t) {
                    case 0: board[i][j] = true;  break;
                    case 1: board[i][j] = false; break;
                    case 2: sx = i; sy = j;      break;
                    case 3: ex = i; ey = j;      break;
                    default:                     break;
                }
            }
        }

        board[sx][sy] = true;
        board[ex][ey] = true;

        dfs(sx, sy, 0);

        if (rslt.empty()) {
            cout << -1 << endl;
        } else {
            vector<int>::iterator min = min_element(rslt.begin(), rslt.end());
            cout << *min << endl;
        }

        rslt.clear();
    }

    return 0;
}

/*-------- Functions --------*/
void dfs(int x, int y, int depth)
{
    // base case: reach the exit
    if (x == ex && y == ey) {
        rslt.push_back(depth);
        return;
    }

    // depth limit
    if (depth == 10) return;

    // up
    if (x > 0 && board[x - 1][y]) {
        int xx = x - 1;

        // throw stone
        if (y == ey)
            while (xx > 0 && board[xx - 1][y] && xx != ex) xx--;
        else
            while (xx > 0 && board[xx - 1][y]) xx--;

        if (xx == ex && y == ey) {  // reach the exit
            dfs(xx, y, depth + 1);
        } else if (xx != 0) {
            board[xx - 1][y] = true;    // block disappears
            dfs(xx, y, depth + 1);
            board[xx - 1][y] = false;   // revert for backtracing
        }
    }

    // down
    if (x + 1 < h && board[x + 1][y]) {
        int xx = x + 1;

        // throw stone
        if (y == ey)
            while (xx + 1 < h && board[xx + 1][y] && xx != ex) xx++;
        else
            while (xx + 1 < h && board[xx + 1][y]) xx++;

        if (xx == ex && y == ey) {  // reach the exit
            dfs(xx, y, depth + 1);
        } else if (xx + 1 != h) {
            board[xx + 1][y] = true;    // block disappears
            dfs(xx, y, depth + 1);
            board[xx + 1][y] = false;   // revert for backtracing
        }
    }

    // left
    if (y > 0 && board[x][y - 1]) {
        int yy = y - 1;

        // throw stone
        if (x == ex)
            while (yy > 0 && board[x][yy - 1] && yy != ey) yy--;
        else
            while (yy > 0 && board[x][yy - 1]) yy--;

        if (x == ex && yy == ey) {  // reach the exit
            dfs(x, yy, depth + 1);
        } else if (yy != 0) {
            board[x][yy - 1] = true;    // block disappears
            dfs(x, yy, depth + 1);
            board[x][yy - 1] = false;   // revert for backtracing
        }
    }

    // right
    if (y + 1 < w && board[x][y + 1]) {
        int yy = y + 1;

        // throw stone
        if (x == ex)
            while (yy + 1 < w && board[x][yy + 1] && yy != ey) yy++;
        else
            while (yy + 1 < w && board[x][yy + 1]) yy++;

        if (x == ex && yy == ey) {  // reach the exit
            dfs(x, yy, depth + 1);
        } else if (yy + 1 != w) {
            board[x][yy + 1] = true;    // block disappears
            dfs(x, yy, depth + 1);
            board[x][yy + 1] = false;   // revert for backtracing
        }
    }
}
